﻿package little.bunny;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MyRegister extends Activity{
	private Button btn_register;
	private EditText txtInputUser;
	private EditText txtInputPass;
	private EditText txtInputPassConfirm;
		
	private Button backButton;

	private final String serviceNameSpace = "http://littlebunny.com/";
	private final String method_name = "CustomRegister";
	private final String serviceUrl = "http://www.littlebunny.com.cn/bunny_service/bunny_service.asmx";
	private final String SOAP_ACTION = "http://littlebunny.com/CustomRegister";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_register);
		
		txtInputUser = (EditText) findViewById(R.id.editRegUser);
		txtInputPass = (EditText) findViewById(R.id.editRegPass);
		txtInputPassConfirm = (EditText) findViewById(R.id.editRegPassConfirm);
		
		SharedPreferences.Editor sharedata1 = getSharedPreferences("user_data", 0).edit();  
		sharedata1.clear();
		sharedata1.commit();
		
		btn_register = (Button) findViewById(R.id.btn_register);
		
		btn_register.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				user_login();
			}
		});
				
		backButton = (Button) findViewById(R.id.back_button);
		backButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
	private void user_login() 
	{
		if (txtInputUser.getText().toString().length() == 0) {
			ShowMsg("请输入账号.");
			return;
		}

		if (txtInputPass.getText().toString().length() == 0) {
			ShowMsg("请输入密码.");
			return;
		}
		
		if (txtInputPassConfirm.getText().toString().length() == 0) {
			ShowMsg("请输入确认密码.");
			return;
		}
		
		if (!txtInputPass.getText().toString().equals(txtInputPassConfirm.getText().toString())) {
			ShowMsg("密码和确认密码不一致，请重新输入.");
			return;
		}
		
		if (NetFunction.CheckNetStatus(this)==false)
		{
			ShowMsg("没有找到有效的网络.");
			return;
		}
		
		//Toast.makeText(this, "检查用户注册信息...", Toast.LENGTH_LONG).show();
		
		final ProgressDialog pDialog = ProgressDialog.show(this, "请稍等",
				"注册用户信息...", true);
		
		pDialog.setOnKeyListener(new OnKeyListener() {
			
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_BACK){
					pDialog.dismiss();
				}
				return false;
			}
		});
		Thread networkThread = new Thread() {
			@Override
			public void run() {
							
				SoapObject request = new SoapObject(serviceNameSpace, method_name);
				request.addProperty("user_id", txtInputUser.getText().toString());
				request.addProperty("u_pass", txtInputPass.getText().toString());

				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
				envelope.dotNet = true;
				envelope.setOutputSoapObject(request);
				
				try {
					HttpTransportSE ht = new HttpTransportSE(serviceUrl);
					ht.call(SOAP_ACTION, envelope);

					final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
					final String result = response.toString();
					
					//Toast.makeText(this, "读取完毕.", Toast.LENGTH_LONG).show();
					
					pDialog.dismiss();// 隐藏进度条
					
					runOnUiThread(new Runnable() {
						public void run() {
							//注册失败
							if (result.startsWith("99:", 0))
							{
								new AlertDialog.Builder(MyRegister.this)
								.setTitle("注册用户")
								.setMessage(result.substring(3))
								.setPositiveButton("确定",
										new DialogInterface.OnClickListener() {
											public void onClick(DialogInterface dialog,
													int which) {
												//MyRegister.this.finish();
											}
										}).show();
								//pDialog.setMessage(result.substring(3));
								//ShowMsg(result.substring(3));
							}
							else
							{
								//注册成功，转向
								new AlertDialog.Builder(MyRegister.this)
								.setTitle("注册用户")
								.setMessage("注册成功，下一步绑定会员卡")
								.setPositiveButton("确定",
										new DialogInterface.OnClickListener() {
											public void onClick(DialogInterface dialog,
													int which) {
												
												SharedPreferences.Editor sharedata1 = getSharedPreferences("user_data", 0).edit();  
												sharedata1.putString("name",txtInputUser.getText().toString());
												
												sharedata1.commit();
												
												Intent myIntent=new Intent();
												myIntent.setClass(MyRegister.this, MyBindCard.class);
												
												startActivity(myIntent);
											}
										}).show();
							}
						}
					});
				} catch (Exception e) {
					pDialog.dismiss();
					e.printStackTrace();
					
					ShowMyDialog("网络信号异常","请查看网络连接，稍后再试...");
				}
			}
		};

		networkThread.start();
	}
	
	private void ShowMsg(String msg)
	{
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}
	
	private void ShowMyDialog(String title, String info)
	{
		new AlertDialog.Builder(MyRegister.this)
		.setTitle(title)
		.setMessage(info)
		.setPositiveButton("确定",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
							int which) {
						//MyLogin.this.finish();
					}
				}).show();
	}
}
